﻿using DotNetty.Codecs;
using DotNetty.Codecs.Protobuf;
using DotNetty.Handlers.Logging;
using DotNetty.Transport.Bootstrapping;
using DotNetty.Transport.Channels;
using DotNetty.Transport.Channels.Sockets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace WinChatListen
{
    public delegate void ReceiveMessageEvent(String message);

    public class NettyServer
    {
        public event ReceiveMessageEvent ReceiveMessage;
        private Int32 port = 8888;

        public async void InitBootstrap()
        {
            var bossGroup = new MultithreadEventLoopGroup(1);
            var workerGroup = new MultithreadEventLoopGroup();
            try
            {
                var bootstrap = new ServerBootstrap();
                bootstrap.Group(bossGroup, workerGroup)
                .Channel<TcpServerSocketChannel>()
                .Option(ChannelOption.SoBacklog, 100)
                .Handler(new LoggingHandler("LISN"))
                .ChildHandler(new ActionChannelInitializer<ISocketChannel>(channel =>
                {
                    var pipeline = channel.Pipeline;
                    pipeline.AddLast(new LoggingHandler("CONN"));
                    //pipeline.AddLast(new EchoServerHandler());
                }));
                var bindTask = bootstrap.BindAsync(8888);
                bindTask.Wait();
                Console.WriteLine($"Echo server started and listening on {bindTask.Result.LocalAddress}");
                Console.ReadLine();
            }
            finally
            {
                workerGroup.ShutdownGracefullyAsync().Wait();
                bossGroup.ShutdownGracefullyAsync().Wait();
            }

            //MultithreadEventLoopGroup bossGroup = new MultithreadEventLoopGroup();
            //MultithreadEventLoopGroup workerGroup = new MultithreadEventLoopGroup();
            //ServerBootstrap bootstrap = new ServerBootstrap();
            //try
            //{
            //    bootstrap.Group(bossGroup, workerGroup)
            //        .LocalAddress(port)
            //        .Channel<TcpServerSocketChannel>()
            //        .Option(ChannelOption.SoBacklog, 128)
            //        .ChildOption(ChannelOption.SoKeepalive, true)
            //        .ChildHandler(new ActionChannelInitializer<IChannel>(channel =>
            //        {
            //            IChannelPipeline pipeline = channel.Pipeline;
            //            //pipeline.AddLast(new DelimiterBasedFrameDecoder(1024 * 100, Delimiters.LineDelimiter()));
            //            pipeline.AddLast(new StringDecoder(Encoding.UTF8));
            //            pipeline.AddLast(new StringEncoder(Encoding.UTF8));
            //            //pipeline.AddLast(new WeChatClientHandler(ReceiveMessage));
            //        }));

            //    // Bind and start to accept incoming connections
            //    IChannel boundChannel = await bootstrap.BindAsync();

            //    await boundChannel.CloseAsync();
            //}
            //finally
            //{
            //    await bossGroup.ShutdownGracefullyAsync();
            //    await workerGroup.ShutdownGracefullyAsync();
            //}
        }
    }
}
